import { copy } from './vite-plugin-copy.js';
import { normalizePath } from 'vite';
import { resolve } from 'node:path';
import fs from 'node:fs';

const __dirname = process.cwd();

fs.rmSync(normalizePath(resolve(__dirname, './public/vite.svg')), { force: true, recursive: true });
fs.rmSync(normalizePath(resolve(__dirname, './src/assets/vue.svg')), { force: true, recursive: true });
fs.rmSync(normalizePath(resolve(__dirname, './src/components/HelloWorld.vue')), { force: true, recursive: true });
fs.rmSync(normalizePath(resolve(__dirname, './src/App.vue')), { force: true, recursive: true });
fs.rmSync(normalizePath(resolve(__dirname, './.gitignore')), { force: true, recursive: true });

fs.mkdirSync(normalizePath(resolve(__dirname, './src/router')), { recursive: true });
fs.mkdirSync(normalizePath(resolve(__dirname, './src/config')), { recursive: true });
fs.mkdirSync(normalizePath(resolve(__dirname, './src/views')), { recursive: true });

copy(
  normalizePath(resolve(__dirname, './', 'node_modules/@gis/lib/.jsconfig.json')),
  normalizePath(resolve(__dirname, './jsconfig.json'))
);

copy(
  normalizePath(resolve(__dirname, './', 'node_modules/@gis/lib/.env')),
  normalizePath(resolve(__dirname, './.env'))
);

copy(
  normalizePath(resolve(__dirname, './', 'node_modules/@gis/lib/.env.production')),
  normalizePath(resolve(__dirname, './.env.production'))
);

copy(
  normalizePath(resolve(__dirname, './', 'node_modules/@gis/lib/.prettierrc')),
  normalizePath(resolve(__dirname, './.prettierrc'))
);

copy(
  normalizePath(resolve(__dirname, './', 'node_modules/@gis/lib/.gitignore')),
  normalizePath(resolve(__dirname, './.gitignore'))
);

copy(
  normalizePath(resolve(__dirname, './', 'node_modules/@gis/lib/router/template.js')),
  normalizePath(resolve(__dirname, './src/router/index.js'))
);

copy(
  normalizePath(resolve(__dirname, './', 'node_modules/@gis/lib/config/staticFormOption.js')),
  normalizePath(resolve(__dirname, './src/config/staticFormOption.js'))
);

copy(
  normalizePath(resolve(__dirname, './', 'node_modules/@gis/lib/main.js')),
  normalizePath(resolve(__dirname, './src/main.js'))
);

fs.writeFileSync(
  normalizePath(resolve(__dirname, './vite.config.js')),
  "import defineViteConfig from '@gis/lib/vite.config.js';\n" +
    '\n' +
    'export default defineViteConfig({ __dirname, process });\n',
  { encoding: 'utf8' }
);

fs.readFile(normalizePath(resolve(__dirname, './.gitignore')), { encoding: 'utf8' }, (err, data) => {
  const dirname = __dirname.split(/[\/\\]+/).pop();
  fs.writeFileSync(
    normalizePath(resolve(__dirname, './.gitignore')),
    [data, '/' + dirname, '/' + dirname + '.*'].join('\n'),
    { encoding: 'utf8' }
  );
});
